<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 *
 */
class Openpx extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    public function updateordertime($id = null)
    {
        if (!empty($id)) {
            $ordershopify = Db::name('fedition_ordershopify')
                ->where('id',$id)
                ->select();
        }else {
            $ids = $this->request->request('ids');
            $where = array();
            if (!empty($ids)) {
                $where['accounturl'] = $ids;
            }else{
                //从56店铺开始更新
                $where['accounturl']  = array('egt',56);
            }
            $ordershopify = Db::name('fedition_ordershopify')
                ->order('updateordertime asc')
                ->where($where)
                ->limit('10')
                ->select();
        }
        if (!empty($ordershopify)) {
            foreach ($ordershopify as $t) {
                $yapp_key = '15e7e7b7-f23a-4e00-a9f8-cba67091206a';
                $yformat = 'json';
                $ymethod = 'tr.order.tracking.get';
                list($msec, $sec) = explode(' ', microtime());
                $ytimestamp = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
                $yv='1.0';
                $app_key = 'app_key'.$yapp_key;
                $format = 'format'.$yformat;
                $method = 'method'.$ymethod;
                $timestamp = 'timestamp'.$ytimestamp;
                $v='v'.$yv;

                $orderfulfillments = Db::name('fedition_orderfulfillments')->where('order_id', $t['id'])->find();
//                if (empty($orderfulfillments)) {
//                    $this->error('未找到相关物流单，请更新订单');
//                }
                $data['deliveryOrderNo'] = $orderfulfillments['tracking_number'];
                $data['deliveryOrderNo'] = '4PX3000065414429CN';
//                $data['deliveryOrderNo'] = '4PX3000063684312CN';
                $body = json_encode($data);
                $appSecret = 'f767bd69-f011-43df-b168-f48ff80e8967';
                $sign = $app_key.''.$format.''.$method.''.$timestamp.''.$v.''.$body.''.$appSecret;
                $sign = md5($sign);
                //测试环境
                //$url = 'http://open.sandbox.4px.com/router/api/service';
                //正式环境
                $url = 'http://open.4px.com/router/api/service';
                $url = $url.'?method='.$ymethod.'&app_key='.$yapp_key.'&v='.$yv.'&timestamp='.$ytimestamp.'&format='.$yformat.'&sign='.$sign;
                $data = $this->posturl($url,$data);
                $data = json_decode($data, true);
                header("content-type:text/html;charset=UTF-8");
                dump($data);
                die;

                $row['updateordertime'] = time();
                Db::name('fedition_ordershopify')
                    ->where('id', $t['id'])
                    ->update($row);
            }
            $this->success('操作成功');
        } else {
            $this->error('未找到相关订单');
        }
    }

    public function geturl($url,$AccessToken=null){
        $headerArray =array("Content-type:text/html;charset=UTF-8;","Accept: text/html, */*; q=0.01");
//        if(!empty($AccessToken)){
            $AccessToken1 = 'Set-Cookie:JSESSIONID=F6D8660D18C257B863E78E85E8133E70; Path=/; Secure; HttpOnly';
            $AccessToken2 = 'Set-Cookie:rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Sun, 28-Nov-2021 10:11:52 GMT; SameSite=lax';
            $headerArray[] = $AccessToken1;
            $headerArray[] = $AccessToken2;
//        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function posturl($url,$data = array(),$AccessToken=null){
        $data = (is_array($data)) ? json_encode($data,JSON_UNESCAPED_UNICODE) : $data;
        $headerArray =array("Content-type:application/text/plain;charset=UTF-8;");
        if(!empty($AccessToken)){
            $headerArray[] = $AccessToken;
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}
